package com.arges.web.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.arges.web.common.Constant;
import com.arges.web.common.GsonUtils;
import com.arges.web.domain.bean.FunBean;
import com.arges.web.domain.pojo.Fun;
import com.arges.web.page.PageInfo;
import com.arges.web.page.WebUtils;
import com.arges.web.service.FunManager;
import com.arges.web.service.PatManager;

/**
 * doctor控制层
 * 
 * @author zxj
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/fun")
public class FunController
{

	private Logger LOGGER = LoggerFactory.getLogger(FunController.class);

	@Autowired
	private FunManager funManager;

	@Autowired
	private PatManager patManager;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getPageFun.do")
	public String getPageFun(HttpServletRequest request, ModelMap map)
	{

		PageInfo<FunBean> pageInfo = null;
		String mrn = request.getParameter("search_mrn");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		if (!StringUtils.isNotBlank(mrn))
		{
			mrn = "";
		}
		if (!StringUtils.isNotBlank(startDate))
		{
			startDate = "";
		}
		if (!StringUtils.isNotBlank(endDate))
		{
			endDate = "";
		}

		String pageString = request.getParameter("PageInfo");
		if (StringUtils.isNotBlank(pageString))
		{
			pageInfo = GsonUtils.stringToObject(pageString, PageInfo.class);
		}
		else
		{
			pageInfo = new PageInfo<FunBean>();
		}

		try
		{
			PageInfo<Fun> pojopInfo = WebUtils.beanToPojoPage(pageInfo, Fun.class);
			pageInfo = funManager.getPageFun(pojopInfo, mrn, startDate, endDate);
		}
		catch (Exception e)
		{
			System.out.println("getPageFun is error. e=" + e);
		}
		map.put("chaxun", mrn);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		map.put("funBeanList", pageInfo.getResult());
		pageInfo.setResult(null);
		map.put("funPageInfo", GsonUtils.objectToJson(pageInfo));
		return "pat/fun";
	}

	@RequestMapping(value = "/saveOrUpdate.do")
	@ResponseBody
	public String saveOrUpdate(HttpServletRequest request, FunBean funBean)
	{
		String result = Constant.SUCCESS;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try
		{
			Date date = sdf.parse(funBean.getF_date_String());
			funBean.setF_date(date);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		if (null != funBean.getId())
		{
			Fun fun = new Fun();
			BeanUtils.copyProperties(funBean, fun);
			funManager.update(fun);
		}
		else
		{
			Fun fun = new Fun();
			BeanUtils.copyProperties(funBean, fun);
			funManager.save(fun);
		}

		return result;
	}

	/**
	 * 根据病历号获取患者信息
	 *
	 * @param mrn
	 * @return
	 */
	@RequestMapping(value = "/getFunById.do")
	@ResponseBody
	public FunBean getFunById(HttpServletRequest request)
	{
		String id = request.getParameter("id");
		FunBean funBean = new FunBean();
		// try
		// {
		Fun fun = funManager.get(Long.valueOf(id));
		BeanUtils.copyProperties(fun, funBean);
		// // }
		// // catch (Exception e)
		// {
		// System.out.println("getFunByMrn is error,error is" + e);
		// // funBean = null;
		// //// }
		return funBean;
	}

	/**
	 * 删除、批量删除用户
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deleteByIds.do")
	@ResponseBody
	public String deleteByIds(HttpServletRequest request)
	{
		String result = Constant.SUCCESS;
		String ids = request.getParameter("ids");
		try
		{
			funManager.deleteByIds(ids);
		}
		catch (Exception e)
		{
			System.out.println("delete fun ByIds is error,error is" + e);
			result = Constant.ERROR;
		}
		return result;
	}
}
